home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_06 / 1106112a < prev    next >
Text File  |  1993-03-28  |  924b  |  44 lines

  1. main()
  2.     {
  3.     char *error_ptr;
  4.  
  5.     if ((error_ptr = initialize()) != NULL) 
  6.         {
  7.         fprintf(stderr, "error during %s", error_ptr);
  8.         exit(1)
  9.         }
  10.      // processing ...
  11.     (void) cleanup(2, NULL);
  12.      exit(0);
  13.     }
  14.  
  15. int fd;
  16. char *buf;
  17.  
  18. char *initialized(void)
  19.     {
  20.      if((buf = malloc(BUFSIZ)) == NULL)
  21.           return cleanup(0, "malloc");   
  22.          // uses cleanup for consistency
  23.      if ((fd = open("filename", O_RDONLY)) < 0)
  24.           return cleanup(1, "open");
  25.      if (read(fd, buf, sizeof(buf) < 0)
  26.           return cleanup(2, "read");
  27.      return NULL;
  28.     }
  29.  
  30. char *cleanup(int start_from, char *where_failed)
  31.     {
  32.      switch (start_from) 
  33.         {
  34.     case 2: 
  35.         close(fd);
  36.     case 1:
  37.         free(buf);
  38.         }
  39.     // could instead print error message here 
  40.     // if where_failed ! = Null
  41.      return where_failed;
  42.     }
  43.  
  44.